package com.gwamcc.adduser.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gwamcc.adduser.service.IOperateService;
import com.gwamcc.adduser.vo.ResponseDataModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * Created by Administrator on 2020/6/10.
 */
@Controller
@RequestMapping("/operate")
public class OperateController {

    private static Logger log = LoggerFactory.getLogger(OperateController.class.getName());

    @Resource
    private IOperateService iOperateService;

    @RequestMapping(value = "/allOperate", method = RequestMethod.POST)
    @ResponseBody
    public Object operate(HttpServletRequest request) {
        ResponseDataModel model = new ResponseDataModel(0, "", null);
        Integer type = Integer.parseInt(request.getParameter("type"));
        String data = request.getParameter("data");
        Object obj = null;
        switch(type) {
            case 0:
                JSONObject jsonObj = JSON.parseObject(data);
                String empid = (String)jsonObj.get("empid");
                String account = (String)jsonObj.get("account");
                String name = (String)jsonObj.get("name");
                String orgid = (String)jsonObj.get("orgid");
                String orgname = (String)jsonObj.get("orgname");
                log.error("========== 新增人员机构信息 ========== empid=" + empid);
                obj = iOperateService.addSave(empid, account, name, orgid, orgname);
                model.setStatus(0);
                model.setMessage((String)obj);
                model.setData(null);
                break;
            case 1:
                JSONArray jsonArr = JSONArray.parseArray(data);
                ArrayList<String> empidList = new ArrayList<String>();
                ArrayList<String> orgidList = new ArrayList<String>();
                for(int i=0; i<jsonArr.size(); i++) {
                    JSONObject json = jsonArr.getJSONObject(i);
                    String emp_id = (String)json.get("empid");
                    String org_id = (String)json.get("orgid");
                    empidList.add(emp_id);
                    if(!orgidList.contains(org_id)) {
                        orgidList.add(org_id);
                    }
                }
                obj = iOperateService.delete(empidList, orgidList);
                log.error("========== 删除人员机构信息 ========== empid=" + empidList);
                model.setStatus(0);
                model.setMessage((String)obj);
                model.setData(null);
                break;
            case 2:
                jsonObj = JSON.parseObject(data);
                empid = (String)jsonObj.get("empid");
                account = (String)jsonObj.get("account");
                name = (String)jsonObj.get("name");
                orgid = (String)jsonObj.get("orgid");
                orgname = (String)jsonObj.get("orgname");
                String orgcode = (String)jsonObj.get("orgcode");
                obj = iOperateService.update(empid, account, name, orgid, orgname, orgcode);
                log.error("========== 更新人员机构信息 ========== empid=" + empid);
                model.setStatus(0);
                model.setMessage((String)obj);
                model.setData(null);
                break;
            case 3:
                String whereSql = request.getParameter("whereSql");
                obj = iOperateService.query(whereSql);
                log.error("========== 查询人员机构信息 ========== " + obj);
                model.setStatus(0);
                model.setMessage("人员机构信息查询成功！");
                model.setData(obj);
                break;
        }
        return JSON.toJSON(model);
    }
}
